/*
 * Copyright (C) 2014 Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.bambole.controller.util;

import com.googlecode.bambole.util.SupportedThemesUtil;

import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.model.SelectItem;
import javax.inject.Named;

/**
 *
 * @author Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 */
@Named("themeUtil")
@SessionScoped
public class ThemeUtil implements Serializable {

    private String current;
    private List<String> items;

    private String css;
    private String folderIcon;

    public String getSelected() {
        return current == null ? ConfigurationUtil.getDefaultTheme() : current;
    }

    public void setSelected(String current) {
        this.current = current;
    }

    public List<String> getItems() {
        return items;
    }

    public void setItems(List<String> items) {
        this.items = items;
    }

    public String getCss() {
        return css == null ? "css/" + current + ".css" : css;
    }

    public void setCss(String css) {
        this.css = css;
    }

    public String getFolderIcon() {
        return folderIcon == null ? "/bambole/resources/image/" + current + "_icon_64_folder.png" : folderIcon;
    }

    public void setFolderIcon(String folderIcon) {
        this.folderIcon = folderIcon;
    }

    public void updateSelection() {
        setSelected(current);
        setCss("css/" + current + ".css");
        setFolderIcon("/bambole/resources/image/" + current + "_icon_64_folder.png");
    }

    public SelectItem[] getItemsAvailableSelectOne() {

        SelectItem[] themes = new SelectItem[getItems().size() + 1];

        themes[0] = new SelectItem(null);

        int i = 1;
        for (String entry : getItems()) {
            themes[i++] = new SelectItem(entry);
        }

        return themes;
    }

    @PostConstruct
    public void init() {
        current = getSelected();

        setItems(SupportedThemesUtil.getItems());
    }
}
